<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑</title>

    <!-- 引入自己写的样式 -->
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_edit.css">
    <style>
        .nav .spacer {
            width: 64%;
        }
        .nav a {
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .nav a img {
            transition: transform .2s ease-in-out; /* 添加过渡效果，使图片放大更加平滑 */  
        }
        .nav a img:hover {  
            transform: scale(1.3) translateY(10px); /* 放大图片到1.5倍 */  
        }
    </style>

    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="js/common.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.png" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="myblog_list.html"><img src="img/default.jpg" alt=""></a>
        <a href="blog_list.html">主页</a>
        <a href="javascript:onExit()">注销</a>
    </div>
    <!-- 编辑框容器 -->
    <div class="blog-edit-container">
        <!-- 标题编辑区 -->
        <div class="title">
            <input id="title" type="text" placeholder="在这里写下文章标题">
            <button id="mysub">发布文章</button>
        </div>
        <!-- 创建编辑器标签 -->
        <div id="editorDiv">
            <textarea id="editor-markdown" style="display:none;"></textarea>
        </div>
    </div>

    <!-- 模态框 -->
    <div class="modal fade" id="publishModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">发布文章</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            </div>
            <div class="modal-body">
            <div class="form-group">
                <label for="articleSummary">文章概要：</label>
                <textarea class="form-control" id="articleSummary" rows="5" placeholder="在这里编写文章概要..."></textarea>
                <small id="summaryLength" class="form-text text-muted">0 / 200</small>
            </div>
            </div>
            <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
            <button id="saveArticleBtn" type="button" class="btn btn-primary">发布</button>
            </div>
        </div>
        </div>
    </div>

    <script>
        var editor;
        function initEdit(md){
            // 编辑器设置
            editor = editormd("editorDiv", {
                // 这里的尺寸必须在这里设置. 设置样式会被 editormd 自动覆盖掉. 
                width: "100%",
                // 高度 100% 意思是和父元素一样高. 要在父元素的基础上去掉标题编辑区的高度
                height: "calc(100% - 50px)",
                // 编辑器中的初始内容
                markdown: md,
                // 指定 editor.md 依赖的插件路径
                path: "editor.md/lib/",
                saveHTMLToTextarea: true // 
            });
        }
        initEdit("# 在这里写下一篇博客"); // 初始化编译器的值

        //用于后端归属人验证
        var uid;
        // 将文章详情展现到编辑页
        function showArt(aid) {
            jQuery.ajax({
                url:"/article/detail",
                type:"POST",
                data:{"id": aid},
                success:function(result) {
                    if (result.code == 200 && result.data != null) {
                        var art =result.data
                        //初始化归属人id
                        uid = art.uid;
                        //渲染标题内容
                        jQuery('#title').val(art.title);
                        //渲染博客内容
                        initEdit(art.content);
                        jQuery('#articleSummary').val(art.summary);
                    }
                }
            })
        }

        //如果url中没参数就是更新文章
        function initPage() {
            var aid = getURLParam("id");
            if(aid != "" && aid > 0) {
                //页面按钮
                jQuery('#mysub').text("更新文章");
                //模态框按钮
                jQuery('#saveArticleBtn').text("更新");
                //文章信息
                showArt(aid);
            }
        }
        initPage();

        //注册点击发布/更新文章按钮后弹出模态框的事件
        var mysub = jQuery('#mysub');
        mysub.click(function() {
            $('#publishModal').modal('show');
            $('#articleSummary').on('input', function() {
                var summaryLength = $(this).val().length;
                $('#summaryLength').text(summaryLength + ' / 200');

                // 如果字符数超过200，则禁用发布按钮
                if (summaryLength > 200 || summaryLength < 50) {
                    $('#summaryLength').addClass('text-danger');
                    $('#saveArticleBtn').prop('disabled', true);
                } else {
                    $('#summaryLength').removeClass('text-danger');
                    $('#saveArticleBtn').prop('disabled', false);
                }
            });
        })
        
        //检查是否有空数据
        function checkValidity() {
            var title = jQuery('#title');
            if(title.val() == "") {
                title.focus();
                alert("请先输入标题！")
                return false;
            }
            var content = jQuery('#editor-markdown');
            if(content.val() == "") {
                title.focus();
                alert("请先输入正文！")
                return false;
            }
            var summary = $('#articleSummary');
            if(summary.val() == "") {
                title.focus();
                alert("请先输入概要！")
                return false;
            }
            return true;
        }

        //注册模态框中点击发布/更新文章按钮后弹出模态框的事件
        var saveArticleBtn = jQuery('#saveArticleBtn');
        saveArticleBtn.click(function() {
            checkValidity();
            //获取要发送的参数
            var title = jQuery('#title');
            var content = jQuery('#editor-markdown');
            var summary = $('#articleSummary');
            //检查是更新文章还是发布文章
            var aid = getURLParam("id");
            if(aid != "" && aid > 0) {
                //此时为更新文章
                jQuery.ajax({
                    url: "/article/edit",
                    type: "POST",
                    data: {
                        "id": aid,
                        "title": title.val(),
                        "content": content.val(),
                        "summary": summary.val(),
                        "uid": uid,
                    },
                    success: function(body) {
                        if(body.data!= null && body.data == 1) {
                            alert("修改文章成功，即将跳转详情页...")
                            location.href = "/blog_content.html?id=" + aid;
                        } else {
                            if(body.code == -200) {
                                alert(body.msg);
                                location.href = "/login.html";
                            } else {
                                alert(body.msg);
                            }
                        }
                    },
                    error: function(body) {
                        alert("用户未登陆，即将跳转到登陆页！");
                        location.href = "/login.html";
                    }
                })
            } else {
                //此时为发布文章
                jQuery.ajax({
                    url: "/article/edit",
                    type: "POST",
                    data: {
                        "title": title.val(),
                        "content": content.val(),
                        "summary": summary.val(),
                    },
                    success: function(body) {
                        aid = body.data;
                        if(aid!= null && aid >= 1) {
                            alert("修改文章成功，即将跳转详情页...")
                            location.href = "/blog_content.html?id=" + aid;
                        } else {
                            if(body.code == -200) {
                                alert(body.msg);
                                location.href = "/login.html";
                            } else {
                                alert(body.msg);
                            }
                        }
                    },
                    error: function(body) {
                        alert("用户未登陆，即将跳转到登陆页！");
                        location.href = "/login.html";
                    }
                })
            }
        })

        function onExit() {
            if(confirm("确认退出？")) {
                jQuery.ajax({
                    url: "/user/logout",
                    type: "post",
                    data: {},
                    success: function(body) {
                        location.href = "/login.html";
                    },
                    error: function(err) {
                        alert("用户未登陆，即将跳转到登陆页！");
                        location.href = "/login.html";
                    }
                });
            }
        }
        
        function onExit() {
            if(confirm("确认退出？")) {
                jQuery.ajax({
                    url: "/user/logout",
                    type: "post",
                    data: {},
                    success: function(body) {
                        location.href = "/login.html";
                    },
                    error: function(err) {
                        alert("用户未登陆，即将跳转到登陆页！");
                        location.href = "/login.html";
                    }
                });
            }
        }
    </script>
</body>

</html>